﻿using hm_restaurant.View;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace hm_restaurant
{
    public partial class Frm_main : Form
    {
        public Frm_main()
        {
            InitializeComponent();
        }

        private void Frm_main_Load(object sender, EventArgs e)
        {
            timer1.Enabled = true;
        }

        int i = 0;
        private void getData()
        {
            i++;
            Console.WriteLine(i.ToString());
            JObject jo=getJSON("http://localhost:9002/");
            if (jo != null)
            {
                Console.WriteLine(jo.ToString());
            }

        }


        public JObject getJSON(string url)//传入网址
        {

            JObject jo = new JObject();
            string resultstring = "";
            Encoding encoding = Encoding.UTF8;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);//这里的url指要获取的数据网址
            request.Method = "GET";
            request.Accept = "text/html, application/xhtml+xml, */*";
            request.ContentType = "application/json";
            
            try
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();

                using (StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
                {
                    resultstring = reader.ReadToEnd();
                }
            }
            catch
            {
                MessageBox.Show("远程服务器连接失败");
                timer1.Enabled = false;

            }
            jo = (JObject)JsonConvert.DeserializeObject(resultstring);
            return jo;
             
        }

        /// <summary>
        /// Json 字符串 转换为 DataTable数据集合
        /// </summary>
        /// <param name="json"></param>
        /// <returns></returns>
        

        private void btn_printer_Click(object sender, EventArgs e)
        {
            Frm_printer fr = new Frm_printer();
            fr.Show();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            getData();
        }
    }
}
